package com.zyz.K_集合.List接口实现类三Vector;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * @author: vick
 * @Date: 2025/3/28
 * @Description:
 * @version: 1.0
 */
public class Demo2 {
    private static final int THREAD_COUNT = 100;
    private static final int ADD_COUNT = 1000;
    public static void main(String[] args) throws InterruptedException {
        ArrayList<Integer> integers = new ArrayList<>();
        testListSafe(integers, "ArrayList");

        Vector<Integer> integers1 = new Vector<>();
        testListSafe(integers1, "Vector");
    }

    public static void testListSafe(List<Integer> list, String listType) throws InterruptedException {
        Thread[] threads = new Thread[THREAD_COUNT];

        for (int i = 0; i < THREAD_COUNT; i++) {
            threads[i] = new Thread(()-> {
                for (int j = 0; j < ADD_COUNT; j++) {
                    list.add(j);
                }
            });
            threads[i].start();
        }

        for (Thread thread : threads) {
            thread.join();
        }

        //输出预期元素数量和实际元素数量
        System.out.println(listType + "预期元素数量： " + (THREAD_COUNT * ADD_COUNT));
        System.out.println(listType + "实际元素数量： " + list.size());
    }
}
